package com.jbp.service;
import com.jbp.exception.RRException;
import com.jbp.model.Gift;
import com.jbp.model.GiftLot;
import com.jbp.redis.RedisConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 哎鸭爆率版抽奖工具
 分大小奖池，大户走大奖池，小户走小奖池
 */
@Service
public class LuckyUtil6 {
    @Autowired
    private RedisConfigService redisConfigService;

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private UserService userService;
    /**
     * @param roomId  房间id，推荐房间单独的奖池，个人房间用公池传 0
     * */
    public List<Gift> luck(int roomId, Integer userId, int times, List<GiftLot> list) {
        //系统总爆率
        double winRate = Double.parseDouble(propertyService.getV("luck_win_rate_sys"));
        //个人最高爆率
        double personRateHigh = Double.parseDouble(propertyService.getV("luck_high_usr"));
        //奖池礼物数量
        int poolSize = Integer.parseInt(propertyService.getV("pool_size"));
        int minPoolSize = Integer.parseInt(propertyService.getV("min_pool_size"));
        int minBlood = Integer.parseInt(propertyService.getV("min_blood"));
        int minBloodC1 = Integer.parseInt(propertyService.getV("min_blood_c1"));
        int minBloodB = Integer.parseInt(propertyService.getV("min_blood_b"));
        int minBloodA = Integer.parseInt(propertyService.getV("min_blood_a"));
        double jcBeishu = Double.parseDouble(propertyService.getV("pool_times"));

        //个人投入
        Integer r_pay = (Integer) redisConfigService.get("haoya:luck:upay:"+userId);
        int pay = r_pay!=null ? r_pay : 0;

        //个人产出
        Integer r_income = (Integer) redisConfigService.get("haoya:luck:uincome:"+userId);
        int income = r_income!=null ? r_income : 0;

        //个人总投入
        Integer r_t_pay = (Integer) redisConfigService.get("haoya:luck:utotalpay:"+userId);
        int totalPay = r_t_pay!=null ? r_t_pay : 0;

        //个人总产出
        Integer r_t_income = (Integer) redisConfigService.get("haoya:luck:utotalincome:"+userId);
        int totalIncome = r_t_income!=null ? r_t_income : 0;

        //判断奖池
        String poolName;
        Map<Integer, Integer> map;
        if (totalPay<10000) {
            map = getGiftCoinfigA();
            poolName = "A:";
            roomId = 0;
        } else if (totalPay<100000) {
            map = getGiftCoinfigB();
            poolName = "B:";
            roomId = 0;
        }  else {
            map = getGiftCoinfigC();
            poolName = "C:";
            roomId = 0;
        }

        //系统总产出
        Integer r_allIncome = (Integer) redisConfigService.get("haoya:luck:allincome:"+poolName+roomId);
        int allIncome = r_allIncome!=null ? r_allIncome : 0;

        //系统总投入
        Integer r_allPay = (Integer) redisConfigService.get("haoya:luck:allpay:"+poolName+roomId);
        int allPay = r_allPay!=null ? r_allPay : 0;

        //水池
        Integer r_pool = (Integer) redisConfigService.get("haoya:luck:pool:"+poolName+roomId);
        int pool = r_pool!=null ? r_pool : 0;

        //奖池剩余礼物数量
        Integer r_giftCount = (Integer) redisConfigService.get("haoya:luck:giftcount:"+poolName+roomId);
        int giftCount = r_giftCount!=null ? r_giftCount : 0;

        //补池
        Integer r_patch_pool = (Integer) redisConfigService.get("haoya:luck:patchpool");
        int patchPool = r_patch_pool!=null ? r_patch_pool : 0;

        //生成奖池
        if (giftCount<=minPoolSize) {
            giftCount += poolSize;
            pool += poolSize * (winRate+0.01) * 20;
            patchPool += poolSize*0.005d*20;
            System.out.println("生成奖池："+pool);
        }

        List<Gift> result = new ArrayList<>();
        for (int i = 0; i < times; i++) {
            giftCount --;
            pay += 20;
            totalPay += 20;
            allPay += 20;

            //剩余爆率， 根据剩余爆率控制用户爆率在1.2到personRateHigh之间
            double rate = BigDecimal.valueOf(pool).divide(BigDecimal.valueOf(giftCount*20), 4, BigDecimal.ROUND_HALF_UP).doubleValue();

            //用户最大奖限制
            int biggest = (int) Math.min(pool/jcBeishu, personRateHigh * pay - income);

            //根据爆率计算最大奖
            int c = 0, t=0;
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                c += entry.getValue();
                t += entry.getKey()*entry.getValue();
                double r = t/20d/c;
                if (rate<r) {
                    biggest = Math.min(biggest, entry.getKey());
                    break;
                }
            }

            biggest = Math.min(biggest, 52000);
            biggest = Math.max(biggest, 52);

            //抽奖 波段补
            int luck;
            int lose = (int) (pay*winRate-income);
            double r = 1d*income/pay;
            if (lose>minBlood && biggest>=5200) {
                luck = luckt(biggest, getGiftCoinfigPatch());
                System.out.println("补"+luck);
            } else if (totalPay<=500000 && lose>minBloodC1 && biggest>=5200) {
                luck = luckt(biggest, getGiftCoinfigPatch1());
            } else if (totalPay<=100000 && totalPay>99000 && lose>13000) {
                if (lose>13000+18000) {
                    luck = 18888;
                } else if (lose>13000+9500) {
                    luck = 10001;
                } else {
                    luck = 5200;
                }
                System.out.println("1w补"+luck);
            } else if (totalPay<99000 && lose>minBloodB && biggest>=10001) {
                luck = 10001;
                System.out.println("补"+luck);
            }  else if (totalPay<70000 && lose>18000 && biggest>=5200) {
                luck = 5200;
                System.out.println("补"+luck);
            } else if (totalPay<50000 && lose>13000 && biggest>=3344) {
                luck = 3344;
            } else if (totalPay<30000 && lose>9000 && biggest>=1888) {
                luck = 1888;
            } else if (totalPay<10000 && lose>minBloodA && biggest>=1000) {
                luck = 1000;
            } else if (totalPay<6000 && lose>2800 && biggest>=520) {
                luck = 520;
            } else if (totalPay<2000 && lose>900 && biggest>=520) {
                luck = 200;
            } else if (totalPay<1000 && lose>400 && biggest>=520) {
                luck = 100;
            }  else  {
                luck = luckt(biggest, map);
            }

            //将奖品转化为礼物
            Gift gift = null;
            for (GiftLot giftLot : list) {
                if (giftLot.getGift().getCoin() == luck) {
                    gift = giftLot.getGift();
                    break;
                }
            }

            if (gift==null) {
                throw new RRException("网络错误");
            }

            income += gift.getCoin();
            totalIncome += gift.getCoin();
            allIncome += gift.getCoin();
            pool -= gift.getCoin();

            result.add(gift);

            //波段处理
            int maxPay = 200000;
            if ((totalPay>maxPay && luck>=10001) || pay>maxPay+100000) {
                if (luck==52000) {
                    pay = maxPay;
                    income = (int) (maxPay * winRate);
                } else {
                    long tempPay = pay - maxPay;
                    pay = maxPay;
                    income = (int) (income - tempPay * winRate);
                }
            }
        }

        redisConfigService.set("haoya:luck:allincome:"+poolName+roomId, allIncome);
        redisConfigService.set("haoya:luck:allpay:"+poolName+roomId, allPay);
        redisConfigService.set("haoya:luck:pool:"+poolName+roomId, pool);
        redisConfigService.set("haoya:luck:patchpool", patchPool);
        redisConfigService.set("haoya:luck:giftcount:"+poolName+roomId, giftCount);
        redisConfigService.set("haoya:luck:upay:"+userId, pay);
        redisConfigService.set("haoya:luck:uincome:"+userId, income);
        redisConfigService.set("haoya:luck:utotalpay:"+userId, totalPay);
        redisConfigService.set("haoya:luck:utotalincome:"+userId, totalIncome);
        return result;
    }

    private static void printConfig(Map<Integer, Integer> map) {
        //总价值
        long totalCoin = map.entrySet().stream().mapToInt(e -> e.getKey()*e.getValue()).sum();
        //总数量
        long totalSize = map.values().stream().mapToInt(v->v).sum();
        double zbl = BigDecimal.valueOf(totalCoin).divide(BigDecimal.valueOf(totalSize*20), 6, BigDecimal.ROUND_HALF_UP).doubleValue();

        for (Map.Entry<Integer, Integer> e : map.entrySet()) {
            double bl = BigDecimal.valueOf(e.getValue() * 100).divide(BigDecimal.valueOf(totalSize), 6, BigDecimal.ROUND_HALF_UP).doubleValue();
            double zb = BigDecimal.valueOf(e.getValue() * e.getKey() * 100).divide(BigDecimal.valueOf(totalCoin), 6, BigDecimal.ROUND_HALF_UP).doubleValue();

            System.out.println(e.getKey()+"钻  "+ e.getValue()+"个   "+" 爆率:"+ bl +"    占比:"+zb);
        }
        System.out.println("总数量："+totalSize+"  总价值:"+totalCoin+"  总爆率："+zbl);
    }

    //清空个人记录
    public void clearUserHistory(){
        System.out.println("清个人奖池数据");
        //个人投入
        redisConfigService.removePattern("haoya:luck:upay:*");
        //个人产出
        redisConfigService.removePattern("haoya:luck:uincome:*");
        //个人总投入
        redisConfigService.removePattern("haoya:luck:utotalpay:*");
        //个人总产出
        redisConfigService.removePattern("haoya:luck:utotalincome:*");
        System.out.println("清奖个人池数据完成");
    }

    public void  clearPool(){
        System.out.println("-----清空奖池-----");
        redisConfigService.set("haoya:luck:allincome:A:0", 0);
        redisConfigService.set("haoya:luck:allpay:A:0", 0);
        redisConfigService.set("haoya:luck:pool:A:0", 0);
        redisConfigService.set("haoya:luck:giftcount:A:0", 0);
        redisConfigService.set("haoya:luck:patchpool", 0);

        redisConfigService.set("haoya:luck:allincome:B:0", 0);
        redisConfigService.set("haoya:luck:allpay:B:0", 0);
        redisConfigService.set("haoya:luck:pool:B:0", 0);
        redisConfigService.set("haoya:luck:giftcount:B:0", 0);

        redisConfigService.set("haoya:luck:allincome:C:0", 0);
        redisConfigService.set("haoya:luck:allpay:C:0", 0);
        redisConfigService.set("haoya:luck:pool:C:0", 0);
        redisConfigService.set("haoya:luck:giftcount:C:0", 0);
    }
    private int luckt(double biggest, Map <Integer, Integer> sorce) {
        Map <Integer, Integer> pool = new HashMap<>();
        sorce.keySet().stream().filter(i -> i<=biggest).forEach(k->{
            pool.put(k, sorce.get(k));
        });
        int sum = pool.values().stream().mapToInt(Integer::intValue).sum();
//        System.out.println("sum:"+sum+" blood:"+blood+"  big:"+biggest);
        int i = new Random().nextInt(sum);
        int t = 0;
        for (Map.Entry<Integer, Integer> entry : pool.entrySet()) {
            t += entry.getValue();
            if (i<t) {
                return entry.getKey();
            }
        }
        throw new RuntimeException("抽奖错误");
    }

    Pool poolA = new Pool();
    Pool poolB = new Pool();
    Pool poolC = new Pool();
    @SuppressWarnings("AliControlFlowStatementWithoutBraces")
    void luckTest(UserLuck userLuck, Map <Integer, Integer> out) {
        if (poolA.map==null) {
            poolA.map=getGiftCoinfigA();
        }
        if (poolB.map==null) {
            poolB.map=getGiftCoinfigB();
        }
        if (poolC.map==null) {
            poolC.map=getGiftCoinfigC();
        }
        //总爆率
        double winRate = 1.135;
        //个人最高爆率
        double personRateHigh = 5;

        Pool pl;
        if (userLuck.pay<10000) {
            pl = poolA;
            /*if (pl.pool>0) {
                System.out.println(" 剩余礼物:"+pl.giftCount+"    水池:"+pl.pool +"   爆率："+BigDecimal.valueOf(pl.pool).divide(BigDecimal.valueOf(pl.giftCount*20), 4, BigDecimal.ROUND_HALF_UP).doubleValue());
            }*/
        } else if (userLuck.pay<100000) {
            pl = poolB;
            /*if (pl.pool>0) {
                System.out.println(" 剩余礼物:"+pl.giftCount+"    水池:"+pl.pool +"   爆率："+BigDecimal.valueOf(pl.pool).divide(BigDecimal.valueOf(pl.giftCount*20), 4, BigDecimal.ROUND_HALF_UP).doubleValue());
            }*/
        }  else {
            pl = poolC;
        }

        ArrayList<Integer> lucks = new ArrayList<>();

        //生成奖池
        if (pl.giftCount<=2000) {
            pl.giftCount += pl.giftSize;
            pl.pool += pl.giftSize * (winRate+0.01) * 20;
//                System.out.println("生成奖池："+ pl.pool);
        }

        pl.giftCount --;
        userLuck.pay += 20;
        userLuck.totalPay += 20;
        userLuck.currPay += 20;
        pl.allPay +=20;


        //剩余爆率
        double rate = BigDecimal.valueOf(pl.pool).divide(BigDecimal.valueOf(pl.giftCount*20), 4, BigDecimal.ROUND_HALF_UP).doubleValue();

//      personRateHigh = Math.min(Math.max(3*rate*rate, 1.18), personRateHigh);

        //用户最大奖限制
        int biggest = (int) Math.min(pl.pool/4.2, personRateHigh * userLuck.totalPay - userLuck.totalIncome);


        //根据爆率计算最大奖
        int c = 0, t=0;
        for (Map.Entry<Integer, Integer> entry : pl.map.entrySet()) {
            c += entry.getValue();
            t += entry.getKey()*entry.getValue();
            double r = t/20d/c;
            if (rate<r) {
                biggest = Math.min(biggest, entry.getKey());
                break;
            }
        }

        biggest = Math.min(biggest, 52000);
        biggest = Math.max(biggest, 52);

        //抽奖 波段补
        int luck;
        int lose = (int) (userLuck.pay*winRate-userLuck.income);
        double r = 1d*userLuck.income/userLuck.pay;

        if (lose>38000 && biggest>=5200) {
            luck = luckt(biggest, getGiftCoinfigPatch());
        } else if (userLuck.totalPay<=500000 && lose>25000 && biggest>=5200) {
            luck = luckt(biggest, getGiftCoinfigPatch1());
        } else if (userLuck.totalPay<=100000 && userLuck.totalPay>99000 && lose>13000) {
            if (lose>13000+18000) {
                luck = 18889;
            } else if (lose>13000+9500) {
                luck = 10003;
            } else {
                luck = 5203;
            }
        }  else if (userLuck.totalPay<99000 && lose>30000 && biggest>=10001) {
            luck = 10002;
        }  else if (userLuck.totalPay<70000 && lose>18000 && biggest>=5200) {
            luck = 5201;
        } else if (userLuck.totalPay<50000 && lose>13000 && biggest>=3344) {
            luck = 3343;
        } else if (userLuck.totalPay<30000 && lose>9000 && biggest>=1888) {
            luck = 1887;
        } else if (userLuck.totalPay<10000 && lose>4200 && biggest>=999) {
            luck = 998;
        } else if (userLuck.totalPay<6000 && lose>2800 && biggest>=520) {
            luck = 521;
        } else if (userLuck.totalPay<2000 && lose>900 && biggest>=520) {
            luck = 201;
        } else if (userLuck.totalPay<1000 && lose>400 && biggest>=520) {
            luck = 101;
        } else  {
            luck = luckt(biggest, pl.map);
        }

        lucks.add(luck);
        userLuck.income += luck;
        userLuck.totalIncome += luck;
        userLuck.currIncome += luck;
        pl.allIncome += luck;
        pl.pool -= luck;

        if (out.containsKey(luck)) {
            out.put(luck, out.get(luck)+1);
        } else {
            out.put(luck, 1);
        }

        //波段处理
        int maxPay = 200000;
        if ((userLuck.totalPay>maxPay && luck>=10001) || userLuck.pay>maxPay+100000) {
            if (luck==52000) {
                userLuck.pay = maxPay;
                userLuck.income = (int) (maxPay * winRate);
            } else {
                double ra = BigDecimal.valueOf(userLuck.currIncome).divide(BigDecimal.valueOf(userLuck.currPay), 4, BigDecimal.ROUND_HALF_UP).doubleValue();
//            System.out.println("投入："+userLuck.currPay+" 产出:"+ userLuck.currIncome+"  爆率:"+ra+ " 实际盈利："+(int)(userLuck.currIncome-userLuck.currPay*1.25) + " : "+ luck);
//            System.out.println(" 剩余礼物:"+pl.giftCount+"    水池:"+pl.pool +"   爆率："+BigDecimal.valueOf(pl.pool).divide(BigDecimal.valueOf(pl.giftCount*20), 4, BigDecimal.ROUND_HALF_UP).doubleValue());
                userLuck.currIncome = 0;
                userLuck.currPay = 0;

                long tempPay = userLuck.pay - maxPay;
                userLuck.pay = maxPay;
                userLuck.income = (int) (userLuck.income - tempPay * winRate);
            }
        }
    }

    /**
     * 小号奖池 投入1w钻以内
     * 1 10 20 52 100 200 520 1000 1888 3344 5200 10001 18888 33440 52000
     */
    private static Map <Integer, Integer> getGiftCoinfigA() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(1, 122400);
        map.put(10, 18840);
        map.put(20, 4000);
        map.put(52, 1200);
        map.put(100, 800);
        map.put(200, 500);
        map.put(520, 1500);
        map.put(1000, 500);
        map.put(1888, 100);
        map.put(3344, 80);
        map.put(5200, 100);
        map.put(10001, 4);
        map.put(18888, 2);
        map.put(33440, 1);
        return map;
    }
    /**
     * 中号奖池 投入10w钻以内
     */
    private static Map <Integer, Integer> getGiftCoinfigB() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(1, 126968);
        map.put(10, 16058);
        map.put(20, 3000);
        map.put(52, 900);
        map.put(100, 580);
        map.put(200, 390);
        map.put(520, 1400);
        map.put(1000, 450);
        map.put(1888, 90);
        map.put(3344, 70);
        map.put(5200, 90);
        map.put(10001, 18);
        map.put(18888, 4);
        map.put(33440, 2);
        map.put(52000, 1);
        return map;
    }

    /**
     * 大号奖池 投入20w钻以上
     */
    private static Map <Integer, Integer> getGiftCoinfigC() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(1, 126745);
        map.put(10, 15700);
        map.put(20, 3000);
        map.put(52, 900);
        map.put(100, 550);
        map.put(200, 360);
        map.put(520, 1400);
        map.put(1000, 450);
        map.put(1888, 90);
        map.put(3344, 70);
        map.put(5200, 90);
        map.put(10001, 18);
        map.put(18888, 4);
        map.put(33440, 2);
        map.put(52000, 1);
        return map;
    }

    private static Map <Integer, Integer> getGiftCoinfigPatch1() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(5200, 15);
        map.put(10001, 8);
        map.put(18888, 2);
        map.put(33440, 1);
        return map;
    }

    private static Map <Integer, Integer> getGiftCoinfigPatch() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        map.put(5200, 25);
        map.put(10001, 15);
        map.put(18888, 4);
        map.put(33440, 3);
        map.put(52000, 2);
        return map;
    }

    private static void tjUser(List<UserLuck> userLucks) {
        //总人数
        int totalCount = userLucks.size();
        //盈利人数
        int winCount = (int) userLucks.stream().filter(user->1d*user.totalIncome/user.totalPay>1.17d && user.totalIncome-user.totalPay*1.17d>0).count();
        //总盈利
        int sumWin = userLucks.stream().filter(user -> 1d*user.totalIncome / user.totalPay > 1.17d && user.totalIncome - user.totalPay * 1.17d > 0).mapToInt(user -> (int) (user.totalIncome - user.totalPay * 1.17d)).sum();
        // 平均盈利
        int avgWin = sumWin/winCount;
        //最大盈利
        int maxWin = userLucks.stream().filter(user -> 1d*user.totalIncome / user.totalPay > 1.17d && user.totalIncome - user.totalPay * 1.17d > 0).mapToInt(user -> (int) (user.totalIncome - user.totalPay * 1.17d)).max().getAsInt();

        //亏损人数
        int loseCount = totalCount - winCount;
        //总亏损
        long sumLose = userLucks.stream().filter(user -> (1d*user.totalIncome / user.totalPay <= 1.17d) || (user.totalIncome - user.totalPay * 1.17d < 0)).mapToLong(user -> (int) (user.totalIncome - user.totalPay * 1.17d)).sum();
        //平均亏损
        long avgLose = loseCount==0 ? loseCount : sumLose/loseCount;
        //最大亏损
        int maxLose = userLucks.stream().filter(user -> (1d*user.totalIncome / user.totalPay <= 1.17d) || (user.totalIncome - user.totalPay * 1.17d < 0)).mapToInt(user -> (int) (user.totalIncome - user.totalPay * 1.17d)).min().getAsInt();

        System.out.println("抽奖总人数："+totalCount);
        System.out.println("盈利人数:"+winCount+"  平均盈利:"+avgWin+" 最大盈利："+maxWin);
        System.out.println("亏损人数:"+loseCount+"  平均亏损:"+avgLose+" 最大亏损："+maxLose);

        System.out.println("---用户亏损统计----");
        System.out.println("亏损大于10w钻： "+userLucks.stream().filter(user -> user.totalIncome - user.totalPay * 1.17d < -100000).count());
        System.out.println("亏损5w-10w钻： "+userLucks.stream().filter(user -> user.totalIncome - user.totalPay * 1.17d >= -100000 && user.totalIncome - user.totalPay * 1.17d < -50000).count());
        System.out.println("亏损<5w钻： "+userLucks.stream().filter(user -> user.totalIncome - user.totalPay * 1.17d >= -50000&& user.totalIncome - user.totalPay * 1.17d < 0).count());
        System.out.println("---用户盈利统计----");
        System.out.println("200<盈利<1000钻: "+userLucks.stream().filter(user -> user.totalIncome - user.totalPay * 1.17d >= 200 && user.totalIncome - user.totalPay * 1.17d < 1000).count());
        System.out.println("1000<盈利<2000钻: "+userLucks.stream().filter(user -> user.totalIncome - user.totalPay * 1.17d >= 1000 && user.totalIncome - user.totalPay * 1.17d < 2000).count());
        System.out.println("2000<盈利<5000钻： "+userLucks.stream().filter(user -> user.totalIncome - user.totalPay * 1.17d >= 2000 && user.totalIncome - user.totalPay * 1.17d < 5000).count());
        System.out.println("5000<盈利<10000钻： "+userLucks.stream().filter(user -> user.totalIncome - user.totalPay * 1.17d >= 5000 && user.totalIncome - user.totalPay * 1.17d < 10000).count());
        System.out.println("10000<盈利<100000钻： "+userLucks.stream().filter(user -> user.totalIncome - user.totalPay * 1.17d >= 10000 && user.totalIncome - user.totalPay * 1.17d < 100000).count());

        System.out.println("---用户爆率统计----");
        System.out.println("爆率<0.7： "+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 0.7d).count());
        System.out.println("0.7<爆率<0.8： "+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 0.8d && user.totalIncome*1d / user.totalPay >= 0.7d).count());
        System.out.println("0.8<爆率<0.9： "+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 0.9d && user.totalIncome*1d / user.totalPay >= 0.8d).count());
        System.out.println("0.9<爆率<1： "+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 1d && user.totalIncome*1d / user.totalPay >= 0.9d).count());
        System.out.println("1<爆率<1.1："+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 1.1d && user.totalIncome*1d / user.totalPay >= 1d).count());
        System.out.println("1.1<爆率<1.15："+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 1.15d && user.totalIncome*1d / user.totalPay >= 1.1d).count());
        System.out.println("1.15<爆率<1.2："+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 1.2d && user.totalIncome*1d / user.totalPay >= 1.15d).count());
        System.out.println("1.2<爆率<1.3："+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 1.3d && user.totalIncome*1d / user.totalPay >= 1.2d).count());
        System.out.println("1.3<爆率<1.5："+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 1.5d && user.totalIncome*1d / user.totalPay >= 1.3d).count());
        System.out.println("1.5<爆率<1.8："+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 1.8d && user.totalIncome*1d / user.totalPay >= 1.5d).count());
        System.out.println("1.8<爆率<2.5："+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 2.5d && user.totalIncome*1d / user.totalPay >= 1.8d).count());
        System.out.println("2.5<爆率<5："+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay < 5d && user.totalIncome*1d / user.totalPay >= 2.5d).count());
        System.out.println("5<爆率："+userLucks.stream().filter(user -> user.totalIncome*1d / user.totalPay >= 5d).count());
    }

    public static void main(String[] args) {
        System.out.println("----------小奖池投入1w钻以下----------");
        printConfig(getGiftCoinfigA());
        System.out.println("----------中奖池投入10w钻以下----------");
        printConfig(getGiftCoinfigB());
        System.out.println("---------------大奖池-----------------");
        printConfig(getGiftCoinfigC());
        System.out.println("--------------模拟规则----------------\n" +
                "1. 按117爆率计算用户盈亏\n" +
                "2. 盈利大于500钻停止抽奖\n" +
                "3. 亏损大于20w钻停止抽奖\n" +
                "4. 总投入大于200w钻停止抽奖");
        System.out.println("--------------模拟抽奖-----------------");
        List<UserLuck> userLucks = new ArrayList<>();
        Map <Integer, Integer> result = new LinkedHashMap<>();
        LuckyUtil6 luckyUtil = new LuckyUtil6();
        ArrayList<Double> list = new ArrayList<>();
        int times = 10000;
        for (int i = 0; i < times; i++) {
            UserLuck user = new UserLuck();
            while (true) {
                luckyUtil.luckTest(user, result);
                if (user.totalPay>=100000 || (user.totalIncome-user.totalPay*1.25d>0) || (user.totalIncome-user.totalPay*1.17d<-200000)) {
//                if (user.totalPay>=500000 || (user.totalIncome-user.totalPay*1.25d>200)) {
//                if (user.totalPay>=400000) {
                    break;
                }
            }
            userLucks.add(user);
            double rate  = BigDecimal.valueOf(user.totalIncome).divide(BigDecimal.valueOf(user.totalPay), 4, BigDecimal.ROUND_HALF_UP).doubleValue();
            System.out.println(i+": 投入："+user.totalPay+" 产出:"+user.totalIncome +"  爆率:"+rate+ " 实际盈利："+(int)(user.totalIncome-user.totalPay*1.17));
        }
        tjUser(userLucks);

        System.out.println("------------系统爆率------------");
        System.out.println("A 系统投入:"+luckyUtil.poolA.allPay+" 产出:"+luckyUtil.poolA.allIncome+" 总爆率:"+ ((double)luckyUtil.poolA.allIncome/luckyUtil.poolA.allPay));
        System.out.println("B 系统投入:"+luckyUtil.poolB.allPay+" 产出:"+luckyUtil.poolB.allIncome+" 总爆率:"+ ((double)luckyUtil.poolB.allIncome/luckyUtil.poolB.allPay));
        System.out.println("C 系统投入:"+luckyUtil.poolC.allPay+" 产出:"+luckyUtil.poolC.allIncome+" 总爆率:"+ ((double)luckyUtil.poolC.allIncome/luckyUtil.poolC.allPay));
        System.out.println("总 系统投入:"+(luckyUtil.poolA.allPay+luckyUtil.poolB.allPay+luckyUtil.poolC.allPay)+
                " 产出:"+(luckyUtil.poolA.allIncome+luckyUtil.poolB.allIncome+luckyUtil.poolC.allIncome)+
                " 总爆率:"+ ((double)(luckyUtil.poolA.allIncome+luckyUtil.poolB.allIncome+luckyUtil.poolC.allIncome)/(luckyUtil.poolA.allPay+luckyUtil.poolB.allPay+luckyUtil.poolC.allPay)));


        System.out.println("------------礼物次数统计-----------");
        result.keySet().stream().sorted().forEach(k->{
            System.out.println("抽到:"+k+"   次:"+result.get(k));
        });
    }
}
